public class BinarySearch {
        public static int FindVal(int arrays[],int val){
            int left = 0;
            int right = arrays.length;
            while(left<=right){
                int mid = (left + right) / 2;
                if(val<arrays[mid]){
                    //去左侧区间查找
                    right = mid-1;
                }else if(val<arrays[mid]){
                    //去右侧区间查找
                    left = mid + 1;
                }else{
                    //相等，说明找到了
                    return mid;
                }
            }
            return -1;
        }

        public static void main(String[] args) {
            int[] arrays = {10, 20, 30, 40, 50, 60};
            System.out.println(FindVal(arrays, 40));
        }
}
